#define _CRT_SECURE_NO_WARNINGS

#include "Sort.h"

void StackInit(Stack* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

void STPush(Stack* ps, STDataType x)
{
	assert(ps);

	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		assert(tmp);
		ps->a = tmp;
		ps->capacity = newcapacity;
	}

	ps->a[ps->top++] = x;
}

void STPop(Stack* ps)
{
	assert(ps && !STEmpty(ps));

	ps->top--;
}

bool STEmpty(Stack* ps)
{
	assert(ps);

	return ps->top == 0;
}

int STTop(Stack* ps)
{
	assert(ps && !STEmpty(ps));

	return ps->a[ps->top - 1];
}

void STDestroy(Stack* ps)
{
	assert(ps);

	free(ps->a);
	ps->capacity = ps->top = 0;
}